package com.yanqu.road.server.manager.activity.celebration;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.pb.celebration.CelebrationProto;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description 周年庆活动
 * @Author cwq
 * @Data 2022/4/24 15:54
 */
public class CelebrationMgr extends TempMgr {

    /**
     * 修复进度
     */
    private static Map<Integer, CelebrationActivity> activityMap = new ConcurrentHashMap<>();

    @Override
    public boolean init() throws Exception {
        reload();
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        CelebrationConfigMgr.reloadConfig();
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        getLogger().info("start reload Celebration  activity  start");
        String nowTimeStr = DateHelper.getCurrentDateTimeString();
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.CelebrationActivity.getValue());

        Map<Integer, ActivityInfo> activityInfoMap = ActivityBussiness.getOpenCrossRankActivityInfoMapByCrossServerId(activityTypeList, nowTimeStr, CrossBaseServer.getInstance().getServerId());
        for (ActivityInfo item : activityInfoMap.values()) {
            getCelebrationActivity(item.getActivityId(), true);
        }
        return true;
    }


    public boolean save() {
        for (CelebrationActivity activity : activityMap.values()) {
            activity.save();
        }
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    /**
     * @param activityId
     * @param newIfNull
     * @return
     */
    public static synchronized CelebrationActivity getCelebrationActivity(int activityId, boolean newIfNull) {
        CelebrationActivity celebrationActivity = activityMap.get(activityId);
        if (celebrationActivity == null && newIfNull) {
            // 读配置 获取活动信息
            celebrationActivity = new CelebrationActivity(activityId);
            activityMap.put(activityId, celebrationActivity);
        }
        return celebrationActivity;
    }

    /**
     * @param activityId
     * @return
     */
    public static CelebrationActivity getCelebrationActivity(int activityId) {
        CelebrationActivity celebrationActivity = activityMap.get(activityId);
        if (celebrationActivity == null) {
            synchronized (activityMap) {
                celebrationActivity = activityMap.get(activityId);
                if (celebrationActivity == null) {
                    ActivityInfo activityInfo = ActivityMgr.getActivityInfoMayLoadDB(activityId);
                    if (ActivityHelper.activityInShowTime(activityInfo)) {
                        // 读配置 获取活动信息
                        CelebrationConfigMgr.reloadConfig(activityInfo);
                        celebrationActivity = new CelebrationActivity(activityInfo.getActivityId());
                        activityMap.put(activityId, celebrationActivity);
                    }
                }
            }
        }
        return celebrationActivity;
    }

    /**
     * 进入周年庆
     *
     * @param activityId
     */
    public static CelebrationProto.CelebrationGetProgressRespMsg.Builder enter(int activityId) {
        CelebrationProto.CelebrationGetProgressRespMsg.Builder builder = CelebrationProto.CelebrationGetProgressRespMsg.newBuilder();
        CelebrationActivity celebrationActivity = getCelebrationActivity(activityId);
        if (celebrationActivity == null) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        return celebrationActivity.enter();
    }


    /**
     * 进入周年庆
     *
     * @param activityId
     */
    public static CelebrationProto.CelebrationUsePropCrossToGameMsg.Builder useProp(int activityId, int propId, int propNum) {
        CelebrationProto.CelebrationUsePropCrossToGameMsg.Builder builder = CelebrationProto.CelebrationUsePropCrossToGameMsg.newBuilder();
        builder.setPropId(propId);
        builder.setNum(propNum);
        CelebrationActivity celebrationActivity = getCelebrationActivity(activityId);
        if (celebrationActivity == null) {
            builder.setRet(GameErrorCode.E_ACTIVITY_NO_FOUND);
            return builder;
        }
        celebrationActivity.useProp(builder);
        builder.setRet(0);
        return builder;
    }

    /**
     * 同步周年庆等级
     *
     * @param serverId
     * @param playerId
     */
    public static void synBuildLv(int activityId, long serverId, long playerId) {
        CelebrationActivity celebrationActivity = getCelebrationActivity(activityId);
        if (celebrationActivity != null) {
            celebrationActivity.synBuildLv(serverId,playerId);
        }
    }


}
